// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios';
import VueAxios from 'vue-axios';
import store from './store/index.js'
import './config/rem'
import 'vant/lib/index.css';
import { wxLogin } from '../src/request/api'
import FastClick from 'fastclick' // 引入FastClick
import { Popup, Toast, Lazyload } from 'vant';
Vue.use(Popup).use(Toast);
Vue.use(VueAxios, axios)

/**
 *对Date的扩展，将 Date 转化为指定格式的String
 *月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
 *年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 *例子：
 *(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 *(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
 */
Date.prototype.$format = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


// 使用 fastclick
// FastClick.attach(document.body); 

Vue.use(Lazyload, {
    lazyComponent: true,
    error: require('@/assets/img_err.png'),
});

// 提示框
Vue.prototype.$tip = msg => Toast({
    message: msg,
    forbidClick: true,
    duration: 1500
});
// 加载提示
Vue.prototype.$loading = msg => Toast.loading({
    message: msg ? msg : '请稍后...',
    forbidClick: true,
    duration: 0
});

let url = 'http://mbgo.0791jr.com';
// 分享进入
// http://mbgo.0791jr.com?qrcode=W6FL9QKW&goods_id=89&goods_type=0
if (window.location.href.indexOf('?qrcode=') != -1) {
    let ptel = window.location.href.split('?qrcode=')[1];
    // 邀请码 qrcode
    if (window.location.href.indexOf('&goods_id') != -1) {

        // 判断是否秒杀
        if (ptel.indexOf('&actid=') != -1) {
            // 获取秒杀活动id
            let actId = ptel.split('&actid=')[1];
            if (actId.indexOf('&act_type=') != -1) {
                actId = actId.split('&act_type=')[0];
            }
            sessionStorage.setItem('actId', actId)
        }

        // 判断商品类型
        if (ptel.indexOf('&act_type=') != -1) {
            // 获取秒杀活动id
            let actType = ptel.split('&act_type=')[1];
            if (actType.indexOf('#') != -1) {
                actType = actType.split('#')[0];
            }
            sessionStorage.setItem('actType', actType)
        }

        // 获取商品id
        let uid = ptel.split('&goods_id')[0];
        let goods_id = ptel.split('&goods_id=')[1].split('&goods_type=')[0];
        // 获取商品类型
        let goods_type = ptel.split('&goods_type=')[1];
        if (goods_type.indexOf('#') != -1) {
            goods_type = goods_type.split('#')[0];
            if (goods_type.indexOf('&') != -1) {
                goods_type = goods_type.split('&')[0];
                sessionStorage.setItem('goods_type', goods_type)
            } else {
                sessionStorage.setItem('goods_type', goods_type)
            }
        }
        sessionStorage.setItem('uid', uid);
        sessionStorage.setItem('goods_id', goods_id);

    } else {
        if (ptel.indexOf('&') != -1) {
            let uid = ptel.split('&')[0]
            sessionStorage.setItem('uid', uid);
        } else {
            if (ptel.indexOf('#') != -1) {
                let uid = ptel.split('#')[0];
                sessionStorage.setItem('uid', uid)
            }
        }
    }
}
if (window.location.href.indexOf('&group_id=') != -1) {
    let ptel = window.location.href.split('?goods_id=')[1];
    let goods_id = ptel.split('&id=')[0]
    let uid = ptel.split('&id=')[1].split('&group_id=')[0];
    let group_id = ptel.split('&group_id=')[1];
    if (group_id.indexOf('#') != -1) {
        group_id = group_id.split('#')[0];
        sessionStorage.setItem('group_id', group_id)
    }
    sessionStorage.setItem('ID', uid);
    sessionStorage.setItem('goods_id', goods_id);
}

let userInfo = {
    avatar: "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eq5sCOxjcBFXgGibDZw6ML48raCeqN260usee1LrjUcYhHPTKK33iaJQhLvjgdjKAKwACcicW2cdIfCg/132",
    id: 17,
    mobile: "15279725035",
    nickname: "Mr.F",
    openid: "om7eFw1i3yelxZ6okjSDP9sZFNAI",
    token: "2dd8babc-25f4-4063-82c5-ad7c13ad30c9",
    username: "Mr.F",
}
localStorage.setItem('userInfo', JSON.stringify(userInfo))
localStorage.setItem('token', "2dd8babc-25f4-4063-82c5-ad7c13ad30c9")


// localStorage.removeItem("token")
router.beforeEach((to, from, next) => {
    Toast.clear();

    let isLogin = localStorage.getItem('token');
    //判断链接有没有code和 缓存userid  有就不走去授权的页面


    // if(window.location.href.indexOf('code') == -1){
    //     window.location.href = `http://open.weixin.qq.com/connect/oauth2/authorize?appid=wx51d55f857fb44996&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
    // }else{
    //     // 有code未登录
    //     if(!isLogin){
    //         //切割链接上的code
    //         let code = window.location.href;
    //         let ptel = sessionStorage.getItem('uid');
    //         console.log(ptel)
    //         code  = code.split('code=')[1].split('&')[0]
    //         //登录
    //         wxLogin({
    //           code:code,
    //           invitation_code: ptel ? ptel : ''
    //         }).then(res=>{
    //           //存储userid
    //           if(res.code == 1){
    //             localStorage.setItem('token',res.data.token);
    //             localStorage.setItem('userInfo',JSON.stringify(res.data));
    //             next();
    //           }else{
    //             window.location.href = `http://open.weixin.qq.com/connect/oauth2/authorize?appid=wx51d55f857fb44996&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
    //             return 
    //           }
    //         })
    //     }
    //     if(isLogin){
    //         next();
    //     }
    // }

    //下一个页面  不写会在一个页面不跳转
    next()
})

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>'
})